home *** CD-ROM | disk | FTP | other *** search
Text File | 2000-09-28 | 5.1 KB | 196 lines | [TEXT/CWIE] |
- /*
- You may incorporate this Apple sample source code into your program(s) without
- restriction. This Apple sample source code has been provided "AS IS" and the
- responsibility for its operation is yours. You are not permitted to redistribute
- this Apple sample source code as "Apple sample source code" after having made
- changes. If you're going to re-distribute the source, we require that you make
- it clear in the source that the code was descended from Apple sample source
- code, but that you've made changes.
-
- PLEASE PLEASE PLEASE DO send detailed bug reports to <devbugs@apple.com>.
- */
-
- #ifndef OLDROUTINELOCATIONS
- # define OLDROUTINELOCATIONS 0
- #elif OLDROUTINELOCATIONS
- # error OLDROUTINELOCATIONS must be FALSE when compiling MoreIsBetter.
- #endif
-
- #ifndef OLDROUTINENAMES
- # define OLDROUTINENAMES 0
- #elif OLDROUTINENAMES
- # error OLDROUTINENAMES must be FALSE when compiling MoreIsBetter.
- #endif
-
- #include <Scrap.h>
- #include <Processes.h>
- #include <Resources.h>
- #include <Patches.h>
- #include <A4Stuff.h>
- #include <Traps.h>
-
- ////////////////////////////////////////////////////////////////////////////////////
-
- static pascal Boolean HaveProcessManager (void)
- {
- return GetToolTrapAddress (_OSDispatch) != GetToolTrapAddress (_Unimplemented);
- }
-
- #define CaseString(c,s) case (c) : (s) = "\p"#c" called from app in background."; break
-
- static pascal void CheckBackground (UInt16 trapWord)
- {
- if (HaveProcessManager ( ))
- {
- ProcessSerialNumber frontPSN;
- ProcessSerialNumber curPSN;
- Boolean sameProcess;
-
- if (!GetFrontProcess (&frontPSN))
- if (!GetCurrentProcess (&curPSN))
- if (!SameProcess (&frontPSN,&curPSN,&sameProcess) && !sameProcess)
- {
- ConstStr255Param trapMessage = nil;
-
- switch (trapWord)
- {
- CaseString (_PutScrap, trapMessage);
- CaseString (_InfoScrap, trapMessage);
- CaseString (_UnloadScrap, trapMessage);
- CaseString (_LoadScrap, trapMessage);
- CaseString (_GetScrap, trapMessage);
- CaseString (_ZeroScrap, trapMessage);
- }
-
- if (trapMessage)
- DebugStr (trapMessage);
- }
- }
- }
-
- ////////////////////////////////////////////////////////////////////////////////////
-
- typedef pascal SInt32 (*vPutScrap) ( SInt32 length,
- ResType theType,
- const void * source );
-
- static vPutScrap gPutScrap;
-
- static pascal SInt32 MyPutScrap ( SInt32 length,
- ResType theType,
- const void * source )
- {
- long preservedA4 = SetCurrentA4 ( );
- SInt32 result = gPutScrap (length,theType,source);
- CheckBackground (_PutScrap);
- SetA4 (preservedA4);
- return result;
- }
-
- ////////////////////////////////////////////////////////////////////////////////////
-
- typedef pascal ScrapStuffPtr (*vInfoScrap) (void);
-
- static vInfoScrap gInfoScrap;
-
- static pascal ScrapStuffPtr MyInfoScrap (void)
- {
- long preservedA4 = SetCurrentA4 ( );
- ScrapStuffPtr result = gInfoScrap ( );
- CheckBackground (_InfoScrap);
- SetA4 (preservedA4);
- return result;
- }
-
- ////////////////////////////////////////////////////////////////////////////////////
-
- typedef pascal SInt32 (*vUnloadScrap) (void);
-
- static vUnloadScrap gUnloadScrap;
-
- static pascal SInt32 MyUnloadScrap (void)
- {
- long preservedA4 = SetCurrentA4 ( );
- SInt32 result = gUnloadScrap ( );
- CheckBackground (_UnloadScrap);
- SetA4 (preservedA4);
- return result;
- }
-
- ////////////////////////////////////////////////////////////////////////////////////
-
- typedef pascal SInt32 (*vLoadScrap) (void);
-
- static vLoadScrap gLoadScrap;
-
- static pascal SInt32 MyLoadScrap (void)
- {
- long preservedA4 = SetCurrentA4 ( );
- SInt32 result = gLoadScrap ( );
- CheckBackground (_LoadScrap);
- SetA4 (preservedA4);
- return result;
- }
-
- ////////////////////////////////////////////////////////////////////////////////////
-
- typedef pascal SInt32 (*vGetScrap) ( Handle hDest,
- ResType theType,
- SInt32 *offset );
-
- static vGetScrap gGetScrap;
-
- static pascal SInt32 MyGetScrap ( Handle hDest,
- ResType theType,
- SInt32 * offset )
- {
- long preservedA4 = SetCurrentA4 ( );
- SInt32 result = gGetScrap (hDest,theType,offset);
- CheckBackground (_GetScrap);
- SetA4 (preservedA4);
- return result;
- }
-
- ////////////////////////////////////////////////////////////////////////////////////
-
- typedef pascal SInt32 (*vZeroScrap) (void);
-
- static vZeroScrap gZeroScrap;
-
- static pascal SInt32 MyZeroScrap (void)
- {
- long preservedA4 = SetCurrentA4 ( );
- SInt32 result = gZeroScrap ( );
- CheckBackground (_ZeroScrap);
- SetA4 (preservedA4);
- return result;
- }
-
- ////////////////////////////////////////////////////////////////////////////////////
-
- static pascal void PatchToolTrap (void *oldAddress, UInt16 trapWord, void *newAddress)
- {
- *((void **) oldAddress) = GetToolTrapAddress (trapWord);
- SetToolTrapAddress (UniversalProcPtr (newAddress), trapWord);
- }
-
- #define PatchScrapTrap(name) \
- PatchToolTrap(&g##name##Scrap,_##name##Scrap,My##name##Scrap)
-
- extern "C" { void __Startup__ (void); }
-
- void main (void)
- {
- long preservedA4 = SetCurrentA4 ( );
-
- PatchScrapTrap (Put);
- PatchScrapTrap (Info);
- PatchScrapTrap (Unload);
- PatchScrapTrap (Load);
- PatchScrapTrap (Get);
- PatchScrapTrap (Zero);
-
- DetachResource (RecoverHandle (Ptr (__Startup__)));
- SetA4 (preservedA4);
- }
-